//
//  UserHeaderView.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/8/14.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "UserHeaderView.h"

@interface UserHeaderView()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *businessNameLabel;
@property (weak, nonatomic) IBOutlet UITapGestureRecognizer *tapGesture;
@property (weak, nonatomic) IBOutlet UITapGestureRecognizer *changeImageGesture;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameLabelConstraint;

@end

@implementation UserHeaderView

- (void)renderViewWithUserModel:(SQUserModel *)userModel {
    
    self.headerImageView.layer.cornerRadius = 35.f;
    self.headerImageView.layer.masksToBounds = YES;
    
    if (!userModel) {
        self.nameLabel.text = @"未登录";
        [self.headerImageView setImage:[UIImage imageNamed:@"头像"]];
        [self.tapGesture addTarget:self action:@selector(login)];
        [self.changeImageGesture removeTarget:self action:@selector(changeAvatar)];
        self.businessNameLabel.hidden = YES;
    } else {
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:userModel.userImage] placeholderImage:[UIImage imageNamed:@"头像"]];
        self.nameLabel.text = [NSString stringWithFormat:@"用户ID：%@", userModel.userName];
        [self.changeImageGesture addTarget:self action:@selector(changeAvatar)];
        [self.tapGesture removeTarget:self action:@selector(login)];
        if (![NSString isNilOrEmpty:userModel.shopName]) {
            self.titleLabel.text = @"商户中心";
            self.nameLabelConstraint.constant = 15.f;
            self.businessNameLabel.hidden = NO;
            self.businessNameLabel.text = [NSString stringWithFormat:@"商户名称：%@", userModel.shopName];
        }
    }    
}

- (void)reloadAvatarWithImage:(UIImage *)image {
    [self.headerImageView setImage:image];
}

- (void)login {
    if ([self.delegate respondsToSelector:@selector(loginAction)]) {
        [self.delegate loginAction];
    }
}

- (void)changeAvatar {
    if ([self.delegate respondsToSelector:@selector(changeAvatarAction)]) {
        [self.delegate changeAvatarAction];
    }
}

@end
